<?php
class Scroller extends PageElement
{
	private $speed = 50;
	private $textWidth;
	private $width;
	private $leftOffset = 0;
	
	public function setSpeed($speed)
	{
		$this->speed = $speed;
	}
	
	public function setTextWidth($width)
	{
		$this->textWidth = $width;
	}
	
	public function setWidth($width)
	{
		$this->width = $width;
	}
	
	public function setLeftOffset($leftOffset)
	{
		$this->leftOffset = $leftOffset;
	}
	
	public function getHTML()
	{
		
		$script = "<script type=\"text/javascript\">\n";
//		$script .= "window.onload = ".$this->name."Init;\n";
//		$script .= "window.onunload = ".$this->name."Stop;\n";
		$script .= "var ".$this->name."interval;\n";
		$script .= "var ".$this->name."width;\n";
		$script .= "var ".$this->name."curPosition=0;\n";
		$script .= "var ".$this->name."speed=".$this->speed.";\n";
		$script .= "\n";
		
		$script .= "function ".$this->name."Init()\n";
		$script .= "{\n";
		$script .= "document.getElementById('".$this->name."').style.width='".$this->textWidth."px';\n";		
//		$script .= "alert(\"Width in Init - \" + document.getElementById('".$this->name."').style.width);\n";
//		$script .= "alert('clientWidth = '+document.getElementById('".$this->name."').clientWidth);\n";
//		$script .= "document.getElementById('".$this->name."').parentNode.style.position='absolute';\n";		
//		$script .= $this->name."width=document.getElementById('".$this->name."').clientWidth;\n";
//		$script .= "alert('this->Width = ".$this->width."');\n";
//		$script .= "alert('".$this->name.".Width = '+document.getElementById('".$this->name."').width);\n";
//		$script .= "alert('scrollWidth = '+document.getElementById('".$this->name."').scrollWidth);\n";
//		$script .= "alert('clientWidth = '+document.getElementById('".$this->name."').clientWidth);\n";
//		$script .= "alert('offsetWidth = '+document.getElementById('".$this->name."').offsetWidth);\n";
		$script .= $this->name."scrollit();\n";
		$script .= "}\n";	
		
		$script .= "\n";
		$script .= "function ".$this->name."Stop()\n";
		$script .= "{\n";
//		$script .= "alert('stop');\n";
		$script .= "clearTimeout(".$this->name."interval);\n";
		$script .= "}\n";	
		
		$script .= "\n";
		$script .= "function ".$this->name."scrollit()\n";
		$script .= "{\n";
//		$script .= "alert('scrollit');\n";
//		$script .= "alert(".$this->name."curPosition);\n";
		$script .= "if (parseInt(".$this->name."curPosition) <= parseInt(".$this->textWidth."))\n";
		$script .= "	{\n";
		$script .= "	".$this->name."curPosition++;\n";
		$script .= "	document.getElementById('".$this->name."').style.clip=\"rect(0px,\" + (".$this->width." + ".$this->name."curPosition) + \"px, 1.4em, \" + ".$this->name."curPosition + \"px)\";\n";
		$script .= "	document.getElementById('".$this->name."').style.left=(".$this->leftOffset." - ".$this->name."curPosition) + \"px\";\n";
//		$script .= "	alert(document.getElementById('".$this->name."').style.width);\n";
//		$script .= "	document.getElementById('".$this->name."MonitorID').innerHTML=document.getElementById('".$this->name."').style.width;\n";
		$script .= "	".$this->name."interval=setTimeout(\"".$this->name."scrollit();\",".$this->name."speed);\n";
		$script .= "	}\n";
		$script .= "else\n";
		$script .= "	{\n";
		$script .= "	".$this->name."curPosition=0;\n";
		$script .= "	".$this->name."interval=setTimeout(\"".$this->name."scrollit();\",".$this->name."speed);\n";
		$script .= "	}\n";
		$script .= "}\n";

		$script .= "\n";
		$script .= "</script>\n";
		
//		$monitor = "<div class=\"MainSectionsScrollerMonitor\" id=\"".$this->name."MonitorID\">Test</div>\n";
		
//		$this->setOnLoad($this->onLoad."".$this->name."init();");
//		$this->setOnUnload($this->onLoad."".$this->name."stopinterval();");
		
		$html = "<div ".parent::getHTML().">\n".$script."\n".$this->text."\n</div>\n";
		return $html;
	}
	
}